import cv2
import numpy as np
from utils.common import load_image, show_image, make_dirs  # 导入公共函数
from utils.config import exp4_global_path, exp4_output_dir  # 导入输出路径

# 确保输出目录存在
make_dirs(exp4_output_dir)

# 加载图片
image = load_image()

# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 全局阈值法
threshold_value = 127  # 阈值
max_value = 255  # 最大值
_, binary_image = cv2.threshold(gray_image, threshold_value, max_value, cv2.THRESH_BINARY)

# 显示结果
cv2.imshow("Global Threshold Binary Image", binary_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存结果
cv2.imwrite(exp4_global_path, binary_image)
print(f"全局阈值分割结果已保存：{exp4_global_path}")